package com.ruoyi.system.controller;

import java.time.Year;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysTeacherClass;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysClass;
import com.ruoyi.system.service.ISysClassService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 排课Controller
 *
 * @author ruoyi
 * @date 2025-06-25
 */
@RestController
@RequestMapping("/system/class")
public class SysClassController extends BaseController
{
    @Autowired
    private ISysClassService sysClassService;

    /**
     * 查询排课列表
     */
    //@PreAuthorize("@ss.hasPermi('system:class:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysClass sysClass)
    {
        startPage();
        List<SysClass> list = sysClassService.selectSysClassList(sysClass);
        return getDataTable(list);
    }

    /**
     * 导出排课列表
     */
    @PreAuthorize("@ss.hasPermi('system:class:export')")
    @Log(title = "排课", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysClass sysClass)
    {
        List<SysClass> list = sysClassService.selectSysClassList(sysClass);
        ExcelUtil<SysClass> util = new ExcelUtil<SysClass>(SysClass.class);
        util.exportExcel(response, list, "排课数据");
    }

    /**
     * 获取排课详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:class:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysClassService.selectSysClassById(id));
    }

    /**
     * 新增排课
     */
    //@PreAuthorize("@ss.hasPermi('system:class:add')")
    @Log(title = "排课", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysClass sysClass)
    {
        return toAjax(sysClassService.insertSysClass(sysClass));
    }

    /**
     * 修改排课
     */
    @PreAuthorize("@ss.hasPermi('system:class:edit')")
    @Log(title = "排课", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysClass sysClass)
    {
        return toAjax(sysClassService.updateSysClass(sysClass));
    }

    /**
     * 删除排课
     */
    @PreAuthorize("@ss.hasPermi('system:class:remove')")
    @Log(title = "排课", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(sysClassService.deleteSysClassById(id));
    }

    /**
     *
     * @return
     */
    @GetMapping("/selectTeacherClass")
    public TableDataInfo selectTeacherClass(SysTeacherClass sysTeacherClass){
        startPage();
        List<SysTeacherClass> teacherClassList = sysClassService.selectTeacherClass(sysTeacherClass);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(teacherClassList);
        tableDataInfo.setTotal(new PageInfo(teacherClassList).getTotal());
        int currentWeekOfYear = 0;
        if (null != sysTeacherClass.getNowWeek()){
            currentWeekOfYear = sysTeacherClass.getNowWeek();
        }
        else if (null == sysTeacherClass.getNowWeek()){
            currentWeekOfYear = sysClassService.getCurrentWeekOfYear();
        }
        tableDataInfo.setNowWeek(currentWeekOfYear);
        tableDataInfo.setWeekRange(sysClassService.getWeekRangeFormatted(Year.now().getValue(),currentWeekOfYear));
        return tableDataInfo;
    }

    /**
     * 确认课时
     * @param sysClass
     * @return
     */
    @GetMapping("/confirmClass")
    public AjaxResult confirmClass(SysClass sysClass){
        return toAjax(sysClassService.confirmClass(sysClass));
    }

    /**
     * 确认课时列表
     * @param sysClass
     * @return
     */
    @GetMapping("/confirmList")
    public TableDataInfo confirmList(SysClass sysClass){
        startPage();
        List<SysClass> sysClassList = sysClassService.confirmList(sysClass);
        return getDataTable(sysClassList);
    }

    /**
     * 教师请假
     * @param sysClass
     * @return
     */
    @GetMapping("/askForLeave")
    public AjaxResult askForLeave(SysClass sysClass){
        return toAjax(sysClassService.askForLeave(sysClass));
    }
}